package com.qf.cx.simplenms.link.rai;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

/**
 * Created by linda on 2016/11/13.
 */
public class JnaLibFactory {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private RaiLibrary raiLibrary;

	public JnaLibFactory() {
		String lib = "rai_";
		{
			String arc = String.valueOf(System.getProperty("sun.arch.data.model"));
			if (arc.equals("32")) {
				lib = "rai_" + "x86";
			} else {
				lib = "rai_" + "x64";
			}
		}
		URL url = this.getClass().getClassLoader().getResource(lib + ".dll");
		File file = null;
		try {
			file = new File(url.toURI());
		} catch (URISyntaxException e) {
			logger.error("", e);
			return;
		}
		NativeLibrary.addSearchPath(lib, file.getParent());
		raiLibrary = (RaiLibrary) Native.loadLibrary(lib, RaiLibrary.class);
		logger.info("load {} Library success!", lib);

	}

	public RaiLibrary getRaiLibrary() {
		return raiLibrary;
	}
}
